package com.uoko.common.collection;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * Map集合的工具类
 * </p>
 *
 * @author xianguo
 * Email xianguo@uoko.com
 * created by 2018/6/6 0006
 */
public class MapUtil {
    public static final List<String> getStringList4Map(Map<String, Object> map, String key) {
        List<String> value = new ArrayList<String>();
        if (null == map) return value;
        Object obj = map.get(key);
        if (null == obj) {
            return value;
        } else if (null != obj && obj instanceof List) {
            value = (List<String>) obj;
        } else {
            return value;
        }
        return value;
    }
    /**
     * 从map中获取Map<String,Object>
     *
     * @param map
     * @param key
     * @return
     */
    public static final Map<String, Object> getStringMap4Map(Map<String, Object> map, String key) {
        Map<String, Object> value = new HashMap<String,Object>();
        if (null == map) {
            return value;
        }
        Object obj = map.get(key);
        if (null == obj) {
            return value;
        } else if (null != obj && obj instanceof Map) {
            value = (Map<String, Object>) obj;
        } else {
            return value;
        }
        return value;
    }

    /**
     * 从map中获取String
     *
     * @param map
     * @param key
     * @return
     */
    public static final String getString4Map(Map<String, Object> map, String key) {
        String value = "";
        if (null == map) {
            return value;
        }
        Object obj = map.get(key);
        if (null == obj) {
            return "";
        } else if (null != obj) {
            value = obj.toString();
        }
        return value;
    }

    /**
     * 从map中获取String
     *
     * @param map
     * @param key
     * @return
     */
    public static final String getString4MapNoBlank(Map<String, Object> map, String key) {
        String value = "";
        if (null == map) {
            return value;
        }
        Object obj = map.get(key);
        if (null == obj) {
            return "";
        } else if (null != obj) {
            value = obj.toString();
        }
        return replaceBlank(value);
    }
    /**
     * 从map中获取int
     *
     * @param map
     * @param key
     * @return
     */
    public static final int getInt4Map(Map<String, Object> map, String key) {
        int value = 0;
        if (null == map) {
            return value;
        }
        Object obj = map.get(key);
        if (null == obj) {
            return 0;
        }
        if (null != obj) {
            value = Integer.parseInt(obj.toString());
        }
        return value;
    }

    /**
     * 从map中获取float
     *
     * @param map
     * @param key
     * @return
     */
    public static final float getFloat4Map(Map<String, Object> map, String key) {
        float value = 0;
        if (null == map) {
            return value;
        }
        Object obj = map.get(key);
        if (null == obj) {
            return 0.0F;
        }
        if (null != obj) {
            value = Float.parseFloat(obj.toString());
        }
        return value;
    }

    /**
     * 从map中获取Date
     *
     * @param map
     * @param key
     * @return
     */
    public static final Date getDate4Map(Map<String, Object> map, String key) {
        Date value = null;
        if (null == map) {
            return value;
        }
        Object obj = map.get(key);
        if (null != obj) {
            value = (Date) obj;
        }
        return value;
    }

    /**
     * 从map中获取Date
     *
     * @param map
     * @param key
     * @return
     */
    public static final Date getDateStr4Map(Map<String, Object> map, String key, String fmt) {
        Date value = null;
        String str = getString4Map(map, key);
        if (null != str && !"".equals(str)) {
            try {
                value = new SimpleDateFormat(fmt).parse(str);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        return value;
    }

    /**
     * 将null转换为字符串""
     *
     * @param map
     * @return
     */
    public static final Map<String, Object> conversMapNull2String(Map<String, Object> map) {
        Map<String, Object> result = new HashMap<String, Object>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            result.put(entry.getKey(), (null == entry.getValue() ? "" : entry.getValue()));
        }
        return result;
    }
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n|");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest.trim();
    }
}
